var isNewUser = false;
var hasActive = false;
var isBack = true;
var b_Url = $('#b_Url').val();
var acid = $('#ac_id').val();
$(function(){
	
	getMyFlower(acid);
	checkIsNuewUser();   

	//点击给阳光
	$('.btn_givesunshin_1').click(function(){
		var url = '/bhqf/index.php/main/giveSunShine/acid/'+acid;
		$.getJSON(url,function(obj){
			if(obj.s){
				showsunshine(obj.d);
			}else{
				alert(obj.m);
			}
		});
	});
	
	$('#page1 .flist').height($('#page1 .flist').width()/5);
	$('.vflist').height($('.vflist').width()/5);
	$('.btn_dh,.btn_input').height($(window).width()/286*78/2);
	
	$('.btnbox,.btnbox1').height($(window).width()*0.9*0.48/286*78);
	
	$('.tab_title u').click(function(){
		var idx = $(this).attr('d_index');
		if($('.tab_title').hasClass('tab_title'+idx)){
			$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
			$('#infopage').animate({top:'92%'},500);
			return false;
		}
		if(!$('.tab_title').hasClass('tab_title1')&&!$('.tab_title').hasClass('tab_title2')&&!$('.tab_title').hasClass('tab_title3')){
			$('#infopage').animate({top:0},500);
		}
		$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
		$('.tab_title').addClass('tab_title'+idx);
		$('.tab_body .pbody').hide();
		$('#pg_'+idx).show();
		$('.sub_tab').height($('.sub_tab').width()/383*66);
	});
	
	$('.sub_tab .back').unbind('touchstart').on('touchstart',function(){
		$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
		$('#infopage').animate({top:'92%'},500);
		return false;
	});
	
	$('.btn_join_1').click(function(){ 
		location.href = b_Url+'index.php/main/index';
	});
	
	$('.btn_guanzhu').click(function(){
		$('.pop_addfriend').fadeIn();
		$('.pop_addfriend').unbind('click').click(function(){
			$('.pop_addfriend').fadeOut();
		});
	});
	
	$('.sub_tab u').click(function(){
		var idx = $(this).attr('d_index');
		$('.sub_tab').removeClass('sub_tab1').removeClass('sub_tab2').addClass('sub_tab'+idx);
		$('.stab_body').hide();
		if(1==idx){
			$('#phb').show();
		}else{
			$('#log').show();
		}
	});
	
	$($('.sub_tab u')[0]).click();

	$('.btn_helpgetsunshin_1').click(function(){ 
		$('.pop_share').fadeIn();
		Shareconfig.Desc = '百花齐放赢大奖，精彩豪礼先到先得哦~';
		Shareconfig.Link = location.href; 
		Setshareinfo();
		$('.pop_share').unbind('touchstart').on('touchstart',function(){
			$('.pop_share').fadeOut();
		});
	});
	
	$('.btn_duijian_1').click(function(){
		$($('.tab_title u')[1]).click();
	});
	
	var dx, dy;
	var pg_1 = document.getElementById("pg_1");
	touch.on(pg_1, 'touchstart', function(ev){
		dx = dy = 0;
		ev.preventDefault();
	});
	touch.on(pg_1, 'drag', function(ev){ 
	    dx = dx || 0;
		dy = dy || 0;		
	});
	touch.on(pg_1, 'dragend', function(ev){
		dx += ev.x;
	    dy += ev.y;
		if(dy>100&&dx<40){
			$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
			$('#infopage').animate({top:'92%'},500);
		}
	}); 
	var pg_2 = document.getElementById("pg_2");
	touch.on(pg_2, 'touchstart', function(ev){
		dx = dy = 0;
		ev.preventDefault();
	});
	touch.on(pg_2, 'drag', function(ev){ 
	    dx = dx || 0;
		dy = dy || 0;		
	});
	touch.on(pg_2, 'dragend', function(ev){
		dx += ev.x;
	    dy += ev.y;
		if(dy>100&&dx<40){
			$('.tab_title').removeClass('tab_title1').removeClass('tab_title2').removeClass('tab_title3');
			$('#infopage').animate({top:'92%'},500);
		}
	});
	
	//获取排行榜数据
	$('#phbbox').height($(window).height()-155);
	//phbbox = new iScroll('phbbox');
	getPHBList();
	
	//获取阳光值数据
	$('#lgbox').height($(window).height()-125); 
	
	ShareConfig.desc = 'i百联绽放公测，百花齐放看杰伦 联心合友聚阳光';
	ShareConfig.link = b_Url+'index.php/main/share/acid/'+acid;
	setShareInfo();
	
});


function getPHBList(){
	var url ='/bhqf/index.php/main/getPHBList';
	$.getJSON(url,function(obj){
			var html = '<dd>'
			+'	<span><b>'+obj.d.my.user.nickname+'</b><em style="background-image: url('+obj.d.my.user.headimgurl+');"></em></span>'
			+'	<span><b>阳光值：<strong>'+obj.d.my.f_count+'</strong></b></span>'
			+'	<span><b>排名：<strong>'+obj.d.my.paiming+'</strong></b></span>'
			+'</dd>'; 
			$.each(obj.d.list,function(i,d){
				var thtml = '<dd class="ll">'
					+'	<strong>'+ (i<=9?'0'+(i+1):(i+1)) +'</strong><b>阳光值'+d.f_count+'</b><em style="background-image: url('+d.user.headimgurl+');"></em><u>'+d.user.nickname+'</u>'
					+'</dd>';
					html += thtml;
			});
			$('#phbbox dl').append(html);
			$('#haicha').text(obj.d.my.paiming-24>0?obj.d.my.paiming-24:0);
		});
}

function showsunshine(id){
	$('#sunbox').show();
	$('#sun').addClass('sunmove');
	setTimeout(function(){
		$('#light').fadeIn(function(){
			$('#indexfshow u.u.u'+id).addClass('open');
			$('#prizefshow u.u.u'+id).addClass('open');
			$('.btn_givesunshin_1').hide();
			$('.btn_helpgetsunshin_1').show();
		});
	},1000);
}

function checkIsNuewUser(){
	var url = '/bhqf/index.php/main/loginby3rdopenid';
	$.getJSON(url,function(obj){
		isNewUser = obj.isNewUser;
	}); 
}

//获取我发起活动的数据
function getMyFlower(acid){
	var url = '/bhqf/index.php/main/getMyFlower/acid/'+acid;
	$.getJSON(url,function(obj){
		if(obj.s){
			hasActive = true;
			ob = obj.d;
			showFlower([ob.f1,ob.f2,ob.f3,ob.f4,ob.f5]);
			ShareConfig.desc = 'i百联绽放公测，百花齐放看杰伦 联心合友聚阳光';
			setShareInfo();
		}
	});
}

function showFlower(data){
	
	$.each(data,function(i,d){
		if(d==1){
			//设置首页的花色
			$('#indexfshow u.u.u'+(i+1)).addClass('open');
			//设置奖品页面的花色
			$('#prizefshow u.u.u'+(i+1)).addClass('open');
		}
	})
	
}